//
//  RDService.swift
//  test-ios
//
//  Created by wyz5204 on 2023/5/31.
//

import Foundation
import Moya
import SwiftyJSON

enum RDService {
    case login(username : String , password : String , pem: String)
    case getRSAKey
    case getAppList
    case getOrgList
    case getProjectList(orgId : Int)
    case getSessionList
    case pullChatMessage(request : ChatMessageParams)
    case none
}

extension RDService : TargetType {
    var baseURL: URL {
        switch(self){
        case .getAppList :
            return URL(string : "https://open-test.rongdasoft.com/")!
        default:
            return URL(string : "https://ibsaas-test-app.rongdasoft.com/")!
        }
    }
    
    var path: String {
        switch(self){
        case .login :
            return "app/sys/login"
        case .getRSAKey :
            return "app/sys/getRSAKey"
        case .getAppList :
            return "openplat/applet/getAppAppletList"
        case .getOrgList :
            return "app/org/getOrgInfosByUserId"
        case .getProjectList :
            return "app/project/info/v1161/getProjectList"
        case .getSessionList :
            return "app/im/chat/session/v140/getChatSessions"
        case .pullChatMessage :
            return "app/im/chat/message/pullChatMessage"

        default : return ""
        }
    }
    
    var method: Moya.Method {
        switch(self){
        default : return .post
        }
    }
    
    var task: Task {
        var urlParameters = [String: Any]()
        var bodyParameters = [String: Any]()
        
        switch(self){
        case .none :
            return .requestParameters(parameters: urlParameters, encoding:URLEncoding.queryString)
            
        case .login(let username , let password , let pem) :
            let param = LoginParams.init(username: username, password: password, publicKey: pem)
            let request = RDRequest.init(data: param)
            return .requestJSONEncodable(request)
            
            
        case .getRSAKey :
            return .requestParameters(parameters:bodyParameters, encoding: JSONEncoding.default)
            
        case .getAppList :
            return .requestParameters(parameters:bodyParameters, encoding: JSONEncoding.default)
        
        case .getSessionList : // 获取session组
            fallthrough
        case .getOrgList :
            return .requestJSONEncodable(RDRequest.init(data: EmptyParams.init()))
            
        case .getProjectList :
            return .requestJSONEncodable(RDRequest.init(data: ProjectParams.init()))
            
        case .pullChatMessage(let request) :
            return .requestJSONEncodable(RDRequest.init(data: request))
            
        default :
            return .requestParameters(parameters:bodyParameters, encoding: JSONEncoding.default)
        }
    }
    
    var headers: [String : String]? {
        let standard = UserDefaults.standard
        var headers: [String: String] = [:]
        headers["Content-type"] = "application/json;charset=utf-8"
        headers["loginType"] = "ios"
        headers["crmId"] = standard.string(forKey: "crmId")
        headers["token"] = standard.string(forKey: "token")
        headers["ibsaasLanguage"] = "zh"
        headers["v"] = "220"
        
        headers["orgId"] = "0" // 组织ID 可变
        headers["projectId"] = "0" // 项目ID 可变
        headers["userId"] = "0" // user ID 可变
        
        switch self {
        case .none:
            headers["Content-type"] = "application/x-www-form-urlencoded"
        case .getProjectList(let orgId):
            headers["orgId"] = String(orgId)
        default: break
        }
        return headers
    }
}
